
#include "../ui.h"
#include "../app_includes.h"

#define CC_TAB_NUM      (3)

static uint8_t tab_id = 0;
static lv_obj_t *ui_screen;
static lv_obj_t *ui_tabview;
static lv_obj_t *ui_tab[CC_TAB_NUM];
static lv_timer_t *lv_timer;

static void cc_tab0_screen_create(lv_obj_t *tab);
static void cc_tab1_screen_create(lv_obj_t *tab);
static void cc_tab2_screen_create(lv_obj_t *tab);
static void cc_tab0_refresh(lv_timer_t *timer);
static void cc_tab1_refresh(lv_timer_t *timer);
static void cc_tab2_refresh(lv_timer_t *timer);

void lv_cc_close(void)
{
    lv_status_bar_hidden(0);
    lv_timer_del(lv_timer);
}

//app应用入口
lv_obj_t *lv_cc_open(lv_obj_t *screen)
{
    ui_tabview = lv_tab_screen_create(screen, ui_tab, CC_TAB_NUM);
    cc_tab0_screen_create(ui_tab[0]);
    cc_tab1_screen_create(ui_tab[1]);
    cc_tab2_screen_create(ui_tab[2]);

    lv_timer = lv_timer_create(cc_tab0_refresh, 100, NULL);

    lv_status_bar_hidden(1);
    
    return ui_tabview;
}

//tab0
static lv_obj_t *ui_PanelVoltage;
static lv_obj_t *ui_LabelVoltageVal;
static lv_obj_t *ui_LabelVoltageUnit;
static lv_obj_t *ui_PanelCurrent;
static lv_obj_t *ui_LabelCurrentVal;
static lv_obj_t *ui_LabelCurrentUnit;
static lv_obj_t *ui_PanelPower;
static lv_obj_t *ui_LabelPowerVal;
static lv_obj_t *ui_LabelPowerUnit;
static lv_obj_t *ui_PanelUSB;
static lv_obj_t *ui_LabelCCFlag;
static lv_obj_t *ui_LabelDir;
static lv_obj_t *ui_LabelDN;
static lv_obj_t *ui_LabelDNVal;
static lv_obj_t *ui_LabelDPVal;
static lv_obj_t *ui_LabelDP;
static lv_obj_t *ui_LabelProtocol;
static lv_obj_t *ui_LabelTemp;
static lv_obj_t *ui_LabelTempVal;

static void cc_tab0_screen_create(lv_obj_t *tab)
{

    ui_PanelPower = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelPower, 230);
    lv_obj_set_height( ui_PanelPower, 50);
    lv_obj_align( ui_PanelPower, LV_ALIGN_BOTTOM_MID, 0, -50);
    lv_obj_clear_flag( ui_PanelPower, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelPower, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelPower, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelPower, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelPowerVal = lv_label_create(ui_PanelPower);
    lv_obj_set_width( ui_LabelPowerVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPowerVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelPowerVal, LV_ALIGN_RIGHT_MID, -32, 0 );
    lv_label_set_text(ui_LabelPowerVal,"50.010");
    lv_obj_set_style_text_font(ui_LabelPowerVal, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelPowerUnit = lv_label_create(ui_PanelPower);
    lv_obj_set_width( ui_LabelPowerUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPowerUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelPowerUnit, 186 );
    lv_obj_set_y( ui_LabelPowerUnit, 10 );
    lv_obj_set_align( ui_LabelPowerUnit, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelPowerUnit,"W");
    lv_obj_set_style_text_font(ui_LabelPowerUnit, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCurrent = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelCurrent, 230);
    lv_obj_set_height( ui_PanelCurrent, 50);
    lv_obj_set_x( ui_PanelCurrent, 0 );
    lv_obj_set_y( ui_PanelCurrent, -60 );
    lv_obj_align_to( ui_PanelCurrent, ui_PanelPower, LV_ALIGN_OUT_TOP_MID,0, -5 );
    lv_obj_clear_flag( ui_PanelCurrent, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelCurrent, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCurrent, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelCurrent, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelCurrentVal = lv_label_create(ui_PanelCurrent);
    lv_obj_set_width( ui_LabelCurrentVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelCurrentVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelCurrentVal, LV_ALIGN_RIGHT_MID, -32, 0 );
    lv_label_set_text(ui_LabelCurrentVal,"5.0010");
    lv_obj_set_style_text_font(ui_LabelCurrentVal, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelCurrentUnit = lv_label_create(ui_PanelCurrent);
    lv_obj_set_width( ui_LabelCurrentUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelCurrentUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelCurrentUnit, 190 );
    lv_obj_set_y( ui_LabelCurrentUnit, 10 );
    lv_obj_set_align( ui_LabelCurrentUnit, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelCurrentUnit,"A");
    lv_obj_set_style_text_font(ui_LabelCurrentUnit, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDir = lv_label_create(ui_PanelCurrent);
    lv_obj_set_width( ui_LabelDir, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDir, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelDir, LV_ALIGN_LEFT_MID, 0, 0);
    lv_label_set_text(ui_LabelDir,"->");
    lv_obj_set_style_text_font(ui_LabelDir, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelVoltage = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelVoltage, 230);
    lv_obj_set_height( ui_PanelVoltage, 50);
    lv_obj_set_x( ui_PanelVoltage, 0 );
    lv_obj_set_y( ui_PanelVoltage, -115 );
    lv_obj_align_to( ui_PanelVoltage, ui_PanelCurrent, LV_ALIGN_OUT_TOP_MID,0, -5 );
    lv_obj_clear_flag( ui_PanelVoltage, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelVoltage, lv_color_hex(0x60A3E7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelVoltage, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelVoltage, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelVoltageVal = lv_label_create(ui_PanelVoltage);
    lv_obj_set_width( ui_LabelVoltageVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelVoltageVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelVoltageVal, LV_ALIGN_RIGHT_MID, -32, 0 );
    lv_label_set_text(ui_LabelVoltageVal,"20.005");
    lv_obj_set_style_text_font(ui_LabelVoltageVal, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelVoltageUnit = lv_label_create(ui_PanelVoltage);
    lv_obj_set_width( ui_LabelVoltageUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelVoltageUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelVoltageUnit, 190 );
    lv_obj_set_y( ui_LabelVoltageUnit, 10 );
    lv_obj_set_align( ui_LabelVoltageUnit, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelVoltageUnit,"V");
    lv_obj_set_style_text_font(ui_LabelVoltageUnit, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelUSB = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelUSB, 230);
    lv_obj_set_height( ui_PanelUSB, 70);
    lv_obj_set_x( ui_PanelUSB, 0 );
    lv_obj_set_y( ui_PanelUSB, -170 );
    lv_obj_align_to( ui_PanelUSB, ui_PanelPower, LV_ALIGN_OUT_BOTTOM_MID,0, 5 );
    lv_obj_clear_flag( ui_PanelUSB, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelUSB, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelUSB, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelUSB, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelTemp = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelTemp, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelTemp, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelTemp, LV_ALIGN_TOP_LEFT, 0, -5);
    lv_label_set_text(ui_LabelTemp,"Temp");
    lv_obj_set_style_text_color(ui_LabelTemp, lv_color_hex(0x65EBA4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelTemp, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelTemp, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelTempVal = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelTempVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelTempVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelTempVal, ui_LabelTemp, LV_ALIGN_OUT_RIGHT_TOP, 8, 0 );
    lv_label_set_text(ui_LabelTempVal,"27C");
    lv_obj_set_style_text_font(ui_LabelTempVal, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDP = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelDP, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDP, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelDP, ui_LabelTempVal, LV_ALIGN_OUT_RIGHT_TOP, 16, 0 );
    lv_label_set_text(ui_LabelDP,"D+");
    lv_obj_set_style_text_color(ui_LabelDP, lv_color_hex(0x41D080), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDP, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDP, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPVal = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelDPVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelDPVal, ui_LabelDP, LV_ALIGN_OUT_RIGHT_TOP, 8, 0 );
    lv_label_set_text(ui_LabelDPVal,"2.53V");
    lv_obj_set_style_text_font(ui_LabelDPVal, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDN = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelDN, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDN, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelDN, ui_LabelDPVal, LV_ALIGN_OUT_RIGHT_TOP, 16, 0 );
    lv_label_set_text(ui_LabelDN,"D-");
    lv_obj_set_style_text_color(ui_LabelDN, lv_color_hex(0xE7E945), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDN, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDN, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDNVal = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelDNVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDNVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelDNVal, ui_LabelDN, LV_ALIGN_OUT_RIGHT_TOP, 8, 0 );
    lv_label_set_text(ui_LabelDNVal,"0.15V");
    lv_obj_set_style_text_font(ui_LabelDNVal, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelProtocol = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelProtocol, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelProtocol, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelProtocol, LV_ALIGN_BOTTOM_LEFT, 0, 0 );
    lv_label_set_text(ui_LabelProtocol,"PD2.0");
    lv_obj_set_style_text_font(ui_LabelProtocol, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelCCFlag = lv_label_create(ui_PanelUSB);
    lv_obj_set_width( ui_LabelCCFlag, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelCCFlag, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelCCFlag, LV_ALIGN_BOTTOM_RIGHT, 0,0);
    lv_label_set_text(ui_LabelCCFlag,"CC1");
    lv_obj_set_style_text_color(ui_LabelCCFlag, lv_color_hex(0x65EBA4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelCCFlag, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelCCFlag, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

}

//tab1
#define WAVE_POINT_NUM      200
#define WAVE_VALUE_MAX      (4000)
static lv_obj_t *ui_chart;
static lv_chart_series_t *ui_series_wave_1;
static lv_coord_t wave_1[WAVE_POINT_NUM];
static lv_chart_series_t *ui_series_wave_2;
static lv_coord_t wave_2[WAVE_POINT_NUM];
static uint16_t has_show_num = 0;

static void cc_tab1_screen_create(lv_obj_t *tab)
{
    ui_chart = lv_chart_create(tab);
    lv_obj_set_height(ui_chart, 200);
    lv_obj_set_width(ui_chart, lv_pct(100));
    lv_obj_align(ui_chart, LV_ALIGN_CENTER,0, 0);
    lv_chart_set_type(ui_chart, LV_CHART_TYPE_LINE);
    lv_chart_set_point_count(ui_chart,WAVE_POINT_NUM);//图表上点的个数
    lv_chart_set_range(ui_chart, LV_CHART_AXIS_PRIMARY_Y, 0, WAVE_VALUE_MAX);
    lv_chart_set_div_line_count(ui_chart, 9, 9);
    lv_chart_set_axis_tick(ui_chart, LV_CHART_AXIS_PRIMARY_X, 8, 4, 0, 0, false, 50);
    lv_chart_set_axis_tick(ui_chart, LV_CHART_AXIS_PRIMARY_Y, 8, 4, 9, 5, false, 50);
    lv_chart_set_axis_tick(ui_chart, LV_CHART_AXIS_SECONDARY_Y, 10, 5, 0, 0, false, 25);
    lv_obj_set_style_bg_color(ui_chart, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_chart, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_chart, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_size(ui_chart, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    ui_series_wave_1 = lv_chart_add_series(ui_chart, lv_color_hex(0x808080), LV_CHART_AXIS_PRIMARY_Y);
    lv_chart_set_ext_y_array(ui_chart, ui_series_wave_1, wave_1);
    lv_chart_set_series_color(ui_chart, ui_series_wave_1, lv_color_hex(0x00ff00));
    ui_series_wave_2 = lv_chart_add_series(ui_chart, lv_color_hex(0x808080), LV_CHART_AXIS_PRIMARY_Y);
    lv_chart_set_ext_y_array(ui_chart, ui_series_wave_2, wave_2);
    lv_chart_set_series_color(ui_chart, ui_series_wave_2, lv_color_hex(0xff0000));
    
}


//tab2
static lv_obj_t *ui_LabelCurrentVal1;
static lv_obj_t *ui_LabelPowerVal1;
static lv_obj_t *ui_LabelVoltageVal1;
static lv_obj_t *ui_LabelDPVUnit1;
static lv_obj_t *ui_LabelDPIUnit1;
static lv_obj_t *ui_LabelDPPUnit1;
static lv_obj_t *ui_LabelPPSV;
static lv_obj_t *ui_LabelPPSI;
static lv_obj_t *ui_PanelUSet;
static lv_obj_t *ui_EditUSet;
static lv_obj_t *ui_PanelISet;
static lv_obj_t *ui_EditISet;
static lv_obj_t *ui_PanelBtnGroup;
static lv_obj_t *ui_BtnGroup[7];
static uint8_t power_used_num = 0;

static void update_power_option(void);
static void set_pps_event_cb(lv_event_t * e);
static void select_power_event_cb(lv_event_t * e);

static void cc_tab2_screen_create(lv_obj_t *tab)
{    
    ui_LabelDPVUnit1 = lv_label_create(tab);
    lv_obj_set_width( ui_LabelDPVUnit1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPVUnit1, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelDPVUnit1, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_label_set_text(ui_LabelDPVUnit1,"Voltage");
    lv_obj_set_style_text_color(ui_LabelDPVUnit1, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPVUnit1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPVUnit1, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_LabelVoltageVal1 = lv_label_create(tab);
    lv_obj_set_width( ui_LabelVoltageVal1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelVoltageVal1, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelVoltageVal1, ui_LabelDPVUnit1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_LabelVoltageVal1,"20.005");
    lv_obj_set_style_text_font(ui_LabelVoltageVal1, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelVoltageVal1, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPIUnit1 = lv_label_create(tab);
    lv_obj_set_width( ui_LabelDPIUnit1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPIUnit1, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelDPIUnit1, ui_LabelVoltageVal1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_LabelDPIUnit1,"Current");
    lv_obj_set_style_text_color(ui_LabelDPIUnit1, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPIUnit1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPIUnit1, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelCurrentVal1 = lv_label_create(tab);
    lv_obj_set_width( ui_LabelCurrentVal1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelCurrentVal1, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelCurrentVal1, ui_LabelDPIUnit1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_LabelCurrentVal1,"5.0010");
    lv_obj_set_style_text_font(ui_LabelCurrentVal1, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelCurrentVal1, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPPUnit1 = lv_label_create(tab);
    lv_obj_set_width( ui_LabelDPPUnit1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPPUnit1, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelDPPUnit1, ui_LabelCurrentVal1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_LabelDPPUnit1,"Power");
    lv_obj_set_style_text_color(ui_LabelDPPUnit1, lv_color_hex(0x60A3E7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPPUnit1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPPUnit1, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelPowerVal1 = lv_label_create(tab);
    lv_obj_set_width( ui_LabelPowerVal1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPowerVal1, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelPowerVal1, ui_LabelDPPUnit1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_LabelPowerVal1,"000.00");
    lv_obj_set_style_text_font(ui_LabelPowerVal1, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelPowerVal1, 10, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_LabelPPSV = lv_label_create(tab);
    lv_obj_set_width( ui_LabelPPSV, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPPSV, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelPPSV, ui_LabelPowerVal1, LV_ALIGN_OUT_BOTTOM_LEFT ,0, 8);
    lv_label_set_text(ui_LabelPPSV,"PPS V");
    lv_obj_set_style_text_color(ui_LabelPPSV, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelPPSV, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelPPSV, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_PanelUSet = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelUSet, 90);
    lv_obj_set_height( ui_PanelUSet, 30);
    lv_obj_align_to( ui_PanelUSet, ui_LabelPPSV, LV_ALIGN_OUT_BOTTOM_LEFT ,0, -4);
    lv_obj_clear_flag( ui_PanelUSet, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelUSet, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelUSet, lv_color_hex(0xcccccc), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_EditUSet = lv_spinbox_create(ui_PanelUSet);
    lv_obj_set_width( ui_EditUSet, 80);
    lv_obj_set_height( ui_EditUSet, 60);
    lv_obj_align( ui_EditUSet, LV_ALIGN_TOP_LEFT, -20, -18);
    lv_spinbox_set_digit_format( ui_EditUSet, 4, 2);
    lv_spinbox_set_range( ui_EditUSet, 0, 2100);
    lv_spinbox_set_step( ui_EditUSet, 2);
    lv_spinbox_set_value( ui_EditUSet, 500);
    lv_obj_set_style_bg_opa(ui_EditUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_EditUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_EditUSet, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_EditUSet, 0, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_EditUSet, LV_TEXT_ALIGN_CENTER, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_decor(ui_EditUSet, LV_TEXT_DECOR_UNDERLINE, LV_PART_CURSOR| LV_STATE_DEFAULT);
    
    ui_LabelPPSI = lv_label_create(tab);
    lv_obj_set_width( ui_LabelPPSI, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPPSI, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelPPSI, ui_PanelUSet, LV_ALIGN_OUT_BOTTOM_LEFT ,0, 0);
    lv_label_set_text(ui_LabelPPSI,"PPS A");
    lv_obj_set_style_text_color(ui_LabelPPSI, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelPPSI, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelPPSI, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelISet = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelISet, 80);
    lv_obj_set_height( ui_PanelISet, 30);
    lv_obj_align_to( ui_PanelISet, ui_LabelPPSI, LV_ALIGN_OUT_BOTTOM_LEFT ,0, -4);
    lv_obj_clear_flag( ui_PanelISet, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelISet, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelISet, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_EditISet = lv_spinbox_create(ui_PanelISet);
    lv_obj_set_width( ui_EditISet, 80);
    lv_obj_set_height( ui_EditISet, 60);
    lv_obj_align( ui_EditISet, LV_ALIGN_TOP_LEFT, -20, -18);
    lv_spinbox_set_digit_format( ui_EditISet, 4, 2);
    lv_spinbox_set_range( ui_EditISet, 0, 500);
    lv_spinbox_set_step( ui_EditISet, 5); //50mA
    lv_spinbox_set_value( ui_EditISet, 300 );
    lv_obj_set_style_bg_opa(ui_EditISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_EditISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_EditISet, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_clear_flag(ui_EditISet,LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(ui_EditISet, 0, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_EditISet, LV_TEXT_ALIGN_CENTER, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_decor(ui_EditISet, LV_TEXT_DECOR_UNDERLINE, LV_PART_CURSOR| LV_STATE_DEFAULT);
    
    ui_PanelBtnGroup = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelBtnGroup, 130);
    lv_obj_set_height( ui_PanelBtnGroup, 220);
    lv_obj_align( ui_PanelBtnGroup, LV_ALIGN_CENTER, 55, 0 );
    lv_obj_set_flex_flow(ui_PanelBtnGroup,LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(ui_PanelBtnGroup, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
    lv_obj_add_flag( ui_PanelBtnGroup, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_scrollbar_mode(ui_PanelBtnGroup, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_bg_color(ui_PanelBtnGroup, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_border_side(ui_PanelBtnGroup, LV_BORDER_SIDE_LEFT, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_PanelBtnGroup, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_PanelBtnGroup, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui_PanelBtnGroup, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_PanelBtnGroup, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_row(ui_PanelBtnGroup, 6, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_column(ui_PanelBtnGroup, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    update_power_option();
    
    lv_obj_add_event_cb(ui_EditUSet, set_pps_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_EditISet, set_pps_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static void update_power_option(void)
{
    uint8_t i;
    static lv_obj_t *ui_label;
    lv_obj_clean(ui_PanelBtnGroup);
    power_used_num = app_pt.power_list_num;
    for(i=0;i!=power_used_num;i++) {
        ui_BtnGroup[i] = lv_btn_create(ui_PanelBtnGroup);
        lv_obj_set_width( ui_BtnGroup[i], 110);
        lv_obj_set_height( ui_BtnGroup[i], 25);
        lv_obj_add_flag( ui_BtnGroup[i], LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
        lv_obj_clear_flag( ui_BtnGroup[i], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
        lv_obj_set_style_bg_color(ui_BtnGroup[i], lv_color_hex(0x2F4F2F), LV_PART_MAIN | LV_STATE_DEFAULT );
        lv_obj_set_style_bg_opa(ui_BtnGroup[i], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        
        ui_label = lv_label_create(ui_BtnGroup[i]);
        lv_obj_set_width( ui_label, LV_SIZE_CONTENT);  /// 1
        lv_obj_set_height( ui_label, LV_SIZE_CONTENT);   /// 1
        lv_obj_set_align( ui_label, LV_ALIGN_CENTER );
        lv_label_set_text(ui_label,app_pt.power_list[i]);
        lv_obj_set_style_text_color(ui_label, lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT );
        lv_obj_set_style_text_opa(ui_label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_text_font(ui_label, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
        
        if(i==app_pt.protocol.power_data_obj_selected) {
            lv_obj_add_state(ui_BtnGroup[i], LV_STATE_CHECKED);
        }
        lv_obj_add_event_cb(ui_BtnGroup[i], select_power_event_cb, LV_EVENT_CLICKED, NULL);//添加事件
    }
    
}

static uint32_t period[] = {100, 1, 100};
static lv_timer_cb_t time_cb[] = {cc_tab0_refresh, cc_tab1_refresh,cc_tab2_refresh};

void cc_group_select(uint8_t id)
{
    uint8_t i;
    
    if(id>=CC_TAB_NUM)
        return;
    //lv_group_remove_all_objs(g_encoder);
    lv_timer_pause(lv_timer);
    lv_timer_set_period(lv_timer,period[id]);
    lv_timer_set_cb(lv_timer, time_cb[id]);
    lv_timer_resume(lv_timer);
    
    switch(id){ //update group
    case 0:  
        break;
    case 1:
        break;
    case 2:
        for(i=0;i!=power_used_num;i++) {
            lv_group_add_obj(g_encoder, ui_BtnGroup[i]);
            if(i==app_pt.protocol.power_data_obj_selected)
                lv_group_focus_obj(ui_BtnGroup[i]);
        }
        lv_group_add_obj(g_encoder, ui_EditUSet);
        lv_group_add_obj(g_encoder, ui_EditISet);
        break;
    default:
        break;
    }
}

static void cc_tab0_refresh(lv_timer_t *timer)
{
    char str[16];
    if(app_dev.meter.power_i.value<0) {
        lv_label_set_text(ui_LabelDir, "<-");
    }
    else {
        lv_label_set_text(ui_LabelDir, "->");
    }
    float_abs_to_str(str, app_dev.meter.power_i.value , 4, 6, NULL);  
    lv_label_set_text(ui_LabelCurrentVal,str);
    float_abs_to_str(str,  app_dev.meter.power_v.value, 4, 7, NULL);  
    lv_label_set_text(ui_LabelVoltageVal,str);
    float_abs_to_str(str, app_dev.meter.power_p.value, 3, 7, NULL);  
    lv_label_set_text(ui_LabelPowerVal,str);
    float_to_str(str, app_dev.meter.dp_v.value , 2, 4, NULL);  
    lv_label_set_text(ui_LabelDPVal,str);
    float_to_str(str, app_dev.meter.dn_v.value , 2, 4, NULL);  
    lv_label_set_text(ui_LabelDNVal,str);
    float_to_str(str, app_dev.meter.cpu_temp.value , 1, 4, NULL);  
    lv_label_set_text(ui_LabelTempVal,str);
    
    if(app_pt.cc==0) 
        lv_label_set_text(ui_LabelCCFlag,"---");
    else if(app_pt.cc==1) 
        lv_label_set_text(ui_LabelCCFlag,"CC1");
    else if(app_pt.cc==2) 
        lv_label_set_text(ui_LabelCCFlag,"CC2");
    lv_label_set_text(ui_LabelProtocol,app_pt.power_list[app_pt.protocol.power_data_obj_selected]);
}

static void cc_tab1_refresh(lv_timer_t *timer)
{
    wave_1[has_show_num] = app_dev.meter.power_v.value_ins*100; //max35.000
    wave_2[has_show_num++] = app_dev.meter.power_i.value_ins*400; //max 10.00A
    if(has_show_num>=WAVE_POINT_NUM)
        has_show_num = 0;
    lv_chart_refresh(ui_chart);
}

static void cc_tab2_refresh(lv_timer_t *timer)
{
    char str[16];

    float_abs_to_str(str, app_dev.meter.power_i.value , 4, 6, NULL);  
    lv_label_set_text(ui_LabelCurrentVal1,str);
    float_abs_to_str(str, app_dev.meter.power_v.value , 3, 6, NULL);  
    lv_label_set_text(ui_LabelVoltageVal1,str);
    float_abs_to_str(str, app_dev.meter.power_p.value , 2, 6, NULL);  
    lv_label_set_text(ui_LabelPowerVal1,str);   
}

static void select_power_event_cb(lv_event_t * e)
{
    uint8_t i;
    lv_obj_t *obj = e->target;
    uint32_t index = lv_obj_get_child_id(obj);
    for(i=0;i!=power_used_num;i++) { //清除标识
        lv_obj_clear_state(ui_BtnGroup[i], LV_STATE_CHECKED);
    }
    lv_obj_add_state(ui_BtnGroup[index], LV_STATE_CHECKED);
    
    app_pt.power_index = index;
    if(app_pt.power_index<=4) {
        if(PD_protocol_select_power(&app_pt.protocol, app_pt.power_index))
            xEventGroupSetBits(pd_event, kPDEvent_PD_OUT);
    }
    else {    
        //app_pt.power_i = lv_spinbox_get_value(ui_EditISet)/100.0;
        //app_pt.power_v = lv_spinbox_get_value(ui_EditUSet)/100.0;
        xEventGroupSetBits(pd_event, kPDEvent_PPS_Start);
    }
}

static void set_pps_event_cb(lv_event_t * e)
{
    if(app_pt.status_power==STATUS_POWER_PPS) {
        app_pt.power_i = lv_spinbox_get_value(ui_EditISet)/100.0;
        app_pt.power_v = lv_spinbox_get_value(ui_EditUSet)/100.0;
        xEventGroupSetBits(pd_event, kPDEvent_PPS_OUT);
    }
}
